<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DbConnection
 * Z tej klazy nie  należy się śmiać, bo to Singleton + Fasada ( Design Paterns rox) i umożliwi ona kontrolę dostępu do bazy danych kiedyś tam jak ją skończę ( czyli nigdy)
 * @author prezes
 */

class DbConnection {
    

    private function readConf(){
        $this->configuration = parse_ini_file('./config/dbconf.ini');
    }
    
    private $configuration;
    private $link;
    private static $instance;
    
    public $stmt;
    
    public static $initialized = false;
    
    private function __clone(){ }
        
    private function __construct() {
        
            $this->readConf();
            $this->link = new mysqli( $this->configuration['hostname'],
                                            $this->configuration['username'],
                                            $this->configuration['password'],
                                            $this->configuration['database'],
                                            $this->configuration['port']);
            
            if($this->link) self::$initialized = true;
    }
    
    function __destruct() {
        
            if($this->link)  $this->link->close();
        
    }
    
    
    
    
    
    
    public static function getInstance(){
        if(self::$instance==null){
            self::$instance = new DbConnection();
        }
        
        return self::$initialized ? self::$instance : null;
    }
    
    public function adminQuery( $query){
        $this->link->query($query, MYSQLI_USE_RESULT);
        return $this->link->error;//>0 ? $this->link->error : null;
        
        
    }
}
